package edu.ynu.se.xiecheng.achitectureclass.common.service;

import edu.ynu.se.xiecheng.achitectureclass.common.entity.CommonRecode;
import edu.ynu.se.xiecheng.achitectureclass.common.utils.TokenManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;

@Service
public class DataFetcherService {

    @Autowired
    private TokenManager tokenManager;

    @Autowired
    private CommonRecodeService commonRecodeService;

    private HashMap<String, CommonRecode> deviceData;

    @Scheduled(fixedRate = 60*1000*10)
    public void fetchData() {
        try {
            String commonToken = tokenManager.getCommonToken();
            deviceData = commonRecodeService.ObtainDataFromServer(commonToken);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public HashMap<String, CommonRecode> getDeviceData() {
        return deviceData;
    }
}
